<?php

namespace App\Http\Controllers\Admin\Shop;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Shop\Category;
use App\Http\Requests\StoreCategory;
use App\Http\Requests\UpdateCategory;
use Illuminate\Support\Facades\Cache;



class CategoryController extends Controller
{

    function __construct()
    {
        view()->share([
            '_category'=>'am-active',
            '_shop'=>'am-in',
            'categories' => Category::get_categories(),
        ]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
//        $categories = Category::where('parent_id', 0)->orderBy('sort_order')->get();
//        foreach ($categories as $k=>$v){
//            $categories[$k]['children'] = Category::where('parent_id',$v['id'])->orderBy('sort_order')->get();
//        }
        return view('admin.shop.category.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.shop.category.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreCategory $request)
    {
        Category::create($request->all());
        Cache::clear();
        return redirect(route('shop.category.index'))->with('success', '新增分类成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $cate = Category::find($id);
        return view('admin.shop.category.edit', compact('cate'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateCategory $request, $id)
    {
        $category = Category::find($id);
        $category->update($request->all());
        Cache::clear();
        return redirect(route('shop.category.index'))->with('success', '编辑成功');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
//        if(!Category::check_children($id)){
//            return back()->with('error','此分类下有二级分类，不能删除');
//        }
        $count = Category::where('parent_id', $id)->count();
        if ($count > 0) {
            $info = array('status' => 0, 'msg' => '此分类下有二级分类，不能删除！');
        } else {
            Category::destroy($id);
            $info = array('status' => 1, 'msg' => '删除成功');
        }
        Cache::clear();
        return $info;

    }

    /***改变属性
     * @param Request $request
     */
    public function change_attr(Request $request)
    {
        Cache::clear();
        $category = Category::find($request->id);
        $attr = $request->attr;
        $category->$attr = !$category->$attr;
        $category->save();

    }

    /***
     * 排序
     */
    public function sort_order(Request $request)
    {
        Cache::clear();
        $category = Category::find($request->id);
        $category->sort_order = $request->sort_order;
        $category->save();
    }

}
